/**
 * @author		Sergey Suzdalskiy
 * @e-mail:		sergey.suzdalskiy@gmail.com
 */
package ua.kiev.gcore.ui.controls
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	public class ListItem extends Sprite
	{
		public var id:int;
		public var label:String;
		public var labelBitmap:Bitmap;
		public var url:String;
		public var size:Point;
		public function ListItem(id:int, data:String, label:String = null, size:Point = null)
		{
			super();
			this.id = id;
			this.url = data;
			this.label = label;
			this.size = size;
			
			if(label){
				var bmp:Bitmap;
				var bmpData:BitmapData;
				var text:TextField = new TextField();
				var format:TextFormat = new TextFormat("Arial", 12, 0xffffff);
				text.defaultTextFormat = format;
				text.text = label;
				text.autoSize = TextFieldAutoSize.LEFT;
				
				bmpData = new BitmapData(text.width, text.height,true,0xffffff);
				bmpData.draw(text);			
				bmp = new Bitmap(bmpData);
				bmp.smoothing = true;
				addChild(bmp);
				
				labelBitmap = new Bitmap(bmpData);
				labelBitmap.smoothing = true;
			}else{
				labelBitmap = new Bitmap();
			}
			if(size){
				graphics.clear();
				graphics.lineStyle(.2,0x666666);
				graphics.beginFill(0xcccccc,.2);
				graphics.drawRect(0,0,size.x, size.y);
				graphics.endFill();
			}
			
			addEventListener(MouseEvent.CLICK, listItemMouseClickHandler);
			addEventListener(MouseEvent.MOUSE_UP, listItemMouseUpHandler);
			addEventListener(MouseEvent.MOUSE_DOWN, listItemMouseDownHandler);
			addEventListener(MouseEvent.MOUSE_OVER, listItemMouseOverHandler);
			addEventListener(MouseEvent.MOUSE_OUT, listItemMouseOutHandler);
		}
		private function listItemMouseClickHandler(event:MouseEvent):void{
			dispatchEvent(new Event(Event.SELECT));
		}
		private function listItemMouseUpHandler(event:MouseEvent):void{
			
		}
		private function listItemMouseDownHandler(event:MouseEvent):void{
			
		}
		private function listItemMouseOverHandler(event:MouseEvent):void{
			if(size){
				graphics.clear();
				graphics.lineStyle(.2,0x666666);
				graphics.beginFill(0x66aacc,.2);
				graphics.drawRect(0,0,size.x, size.y);
				graphics.endFill();
			}
		}
		private function listItemMouseOutHandler(event:MouseEvent):void{
			if(size){
				graphics.clear();
				graphics.lineStyle(.2,0x666666);
				graphics.beginFill(0xcccccc,.2);
				graphics.drawRect(0,0,size.x, size.y);
				graphics.endFill();
			}
		}
	}
}